<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import { ref, onMounted } from 'vue'
import { getApi, addApi, editApi, delApi } from '@/api/book'

onMounted(async () => {
  const res = await getApi()
  tableData.value = res.data.data
})

interface ItableData {
  date: string,
  name: string,
  address: string
}

const tableData: ItableData[] = ref([])

const searchVal = ref('')
const dialogFormVisible = ref(false)
const title = ref('')
const ruleForm = ref({
  id: '',
  bookName: '',
  author: '',
  typeName: '',
  remarks: ''
})
const ruleFormRef = ref<FormInstance>()

const rules = {
  bookName: [
    { required: true, message: '请输入名称', trigger: 'blur' }
  ],
  author: [
    { required: true, message: '请输入作者', trigger: 'blur' }
  ],
  typeName: [
    { required: true, message: '请输入类别', trigger: 'blur' }
  ]
}

const enterSearch = () => {

}
const load = () => {

}
const add = () => {
  dialogFormVisible.value = true
}
const save = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid) => {
    if (valid) {
      // 验证通过，执行保存逻辑
      console.log('表单验证通过')
    }
  })
}
const handleEdit = (row: any, index: number) => {

}
const handleDel = (row: any, index: number) => {

}



</script>
<template>
  <el-card class="main">
    <template #header>
      <el-input style="width:400px" v-model="searchVal" placeholder="名称" @keyup.enter="enterSearch"></el-input>
      <span style="margin-left: 20px;">
        <el-button type="primary" @click="load">查询</el-button>
        <el-button type="primary" @click="add">添加</el-button>
      </span>
    </template>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column type="index" width="50" />
      <el-table-column prop="bookName" label="名称" width="180" />
      <el-table-column prop="author" label="作者" width="180" />
      <el-table-column prop="typeName" label="类别" />
      <el-table-column prop="remarks" label="备注" />
      <el-table-column label="操作">
        <template #default="scope">
          <el-button type="primary" size="small" @click="handleEdit(scope.row,scope.$index)">edit</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.row,scope.$index)">del</el-button>
        </template>
      </el-table-column>
    </el-table>
    <template #footer>
      footer
    </template>
  </el-card>


  <!-- 拟态框 -->
  <el-dialog v-model="dialogFormVisible" :title='title' width="40%">
    <el-form :model="ruleForm" ref="ruleFormRef" :rules="rules">
      <el-form-item label="名称" prop="bookName">
        <el-input v-model="ruleForm.bookName" placeholder="请输入名称" autocomplete="off"/>
      </el-form-item>
      <el-form-item label="作者" prop="author">
        <el-input v-model="ruleForm.author" placeholder="请输入作者" autocomplete="off"/>
      </el-form-item>
      <el-form-item label="类别" prop="typeName">
        <el-input v-model="ruleForm.typeName" placeholder="请输入类别" autocomplete="off"/>
      </el-form-item>
      <el-form-item label="备注" prop="remarks">
        <el-input v-model="ruleForm.remarks" placeholder="请输入备注" autocomplete="off"/>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="dialogFormVisible = false">Cancel</el-button>
      <el-button type="primary" @click="save(ruleFormRef)">
        Confirm
      </el-button>
    </template>
  </el-dialog>
</template>
<style lang="less" scoped>
.main {
  margin: 20px;
}
</style>